import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso #L1正则
from sklearn.linear_model import Ridge #L2正则

from env.Tools.i18n.pygettext import normalize

np.random.seed(666)
x = np.random.uniform(low=-3, high=3, size=100)
X = x.reshape(-1, 1)

y = 0.5* x**2 + x + 2 + np.random.normal(0, 1, size=100)
X2 = np.hstack([X, X**2])
# estimator = LinearRegression()
# estimator.fit(X, y)
# y_predict = estimator.predict(X)

# plt.scatter(x, y)
# plt.plot(x, y_predict, color='red')
# plt.show()

X10 = np.hstack([X2, X**3, X**4, X**5, X**6, X**7, X**8, X**9, X**10])
estimator_l1 = Lasso(alpha=0.005) #正则化强度 alpha
estimator_l1.fit(X10, y)
y_predict_l1 = estimator_l1.predict(X10)

X11 = np.hstack([X2, X**3, X**4, X**5, X**6, X**7, X**8, X**9, X**10])
estimator_l2 = Ridge(alpha=0.005)
estimator_l2.fit(X11, y)
y_predict_l2 = estimator_l2.predict(X11)

plt.scatter(x, y)
plt.plot(np.sort(x), y_predict_l2[np.argsort(x)], color='blue')
# plt.plot(np.sort(x), y_predict_l1[np.argsort(x)], color='red')
plt.show()

print(estimator_l2.coef_)
